package com.nongda.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nongda.dto.ShangPinDto;
import com.nongda.dto.ShangPinPageDto;
import com.nongda.mapper.ShangPinMapper;
import com.nongda.pojo.ShangPin;
import com.nongda.result.PageResult;
import com.nongda.service.ShangPinService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ShangPinServiceImpl implements ShangPinService {
    @Autowired
    ShangPinMapper shangPinMapper;

    @Override
    public void insert(ShangPinDto shangPinDto) {
        ShangPin shangPin=new ShangPin();
        BeanUtils.copyProperties(shangPinDto,shangPin);
        shangPin.setCreateTime(LocalDate.of(2024,1,1));
        shangPin.setCreateCity("武汉xx食品有限公司");
        shangPin.setEatBefore(LocalDate.of(2024,6,1));
        shangPinMapper.tianjia(shangPin);
    }

    @Override
    public void update(ShangPin shangPin) {
        shangPinMapper.xiugai(shangPin);
    }

    @Override
    public void delete(Integer id) {
        shangPinMapper.shanchu(id);
    }

    @Override
    public PageResult page(ShangPinPageDto shangPinPageDto) {
        PageHelper.startPage(shangPinPageDto.getPage(),shangPinPageDto.getPageSize());
        Page<ShangPin> page=shangPinMapper.chaxun(shangPinPageDto);
        List<ShangPin> result = page.getResult();
        PageResult pageResult=new PageResult(shangPinPageDto.getPage(),shangPinPageDto.getPageSize()
        , (int) page.getTotal(),result);

        return pageResult;
    }
}
